﻿/* 
 * Copyright (c) 2011 Jeff Zhang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;

namespace Json {
    public sealed class JsonDictionary : JsonObject<InternalDictionary>, IEnumerable<KeyValuePair<string, IJsonObject>> {
        public static JsonDictionary Empty = new JsonDictionary(0);

        public JsonDictionary() : base(new InternalDictionary()) { }

        public JsonDictionary(int capacity) : base(new InternalDictionary(capacity)) { }

        public override int Length {
            get { return Value.Count; }
        }

        internal void Add(string key, IJsonObject value) {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            if (value == null) {
                value = JsonObject.Null;
            }

            Value.Add(key, value);
        }

        public override IJsonObject this[int index] {
            get {
                if (index < 0 || index >= Value.Count)
                    throw new ArgumentOutOfRangeException("index", "The index must be larger than zero and less than the collection count.");

                return Value[index]; 
            }
            set {
                if (index < 0 || index >= Value.Count)
                    throw new ArgumentOutOfRangeException("index", "The index must be larger than zero and less than the collection count.");

                if (value == null) {
                    value = JsonObject.Null;
                }

                Value[index] = value; 
            }
        }

        public override IJsonObject this[string key] {
            get {
                if (string.IsNullOrEmpty(key))
                    throw new ArgumentNullException("key");

                return Value[key]; 
            }
            set {
                if (string.IsNullOrEmpty(key))
                    throw new ArgumentNullException("key");

                if (value == null) {
                    value = JsonObject.Null;
                }

                Value[key] = value; 
            }
        }

        public bool ContainKey(string key) {
            return Value.Contains(key);
        }

        public override string ToJson() {
            StringBuilder sb = new StringBuilder();

            sb.Append("{");

            string[] keys = Value.Keys;

            for (int i = 0; i < keys.Length; i++) {
                string k = keys[i];
                IJsonObject json = Value[k];

                sb.AppendFormat("{0}:{1},", k, json.ToJson());
            }

            return sb.ToString().TrimEnd(',') + "}";
        }

        public IEnumerator<KeyValuePair<string, IJsonObject>> GetEnumerator() {
            string[] keys = Value.Keys;
            foreach (string key in keys) {
                yield return new KeyValuePair<string, IJsonObject>(key, Value[key]);
            }
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }
    }
}
